package ldh.im.fxgui.controller.sub;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import ldh.im.fxbase.dao.ImDepartmentDao;
import ldh.im.fxbase.dao.ImSessionDao;
import ldh.im.fxbase.data.pojo.ImDepartment;
import ldh.im.fxbase.data.pojo.ImGroup;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.service.ImSessionService;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.ThreadPoolUtil;
import ldh.im.fxgui.controller.BaseController;
import ldh.im.fxgui.util.UiUtil;
import ldh.im.fxgui.util.UserHeadImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 用户管理-》群-》群信息
 */
public class GroupInfoController extends BaseController implements Initializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(GroupInfoController.class);

    @FXML private TextField groupNameTextField;
    @FXML private TextArea groupDescTextArea;

    @FXML private TextField userIdTextField;
    @FXML private TextField mottoTextField;
    @FXML private ImageView headImageView;

    @FXML private TextField sexTextField;
    @FXML private TextField departmentNameTextField;
    @FXML private TextField mobileTextField;
    @FXML private TextField userNameTextField;

    private volatile ImUser imUser;
    private volatile ImGroup imGroup;

    private ImSessionDao sessionDao = BeanFactory.getInstance().getBean(ImSessionDao.class);
    private ImDepartmentDao departmentDao = BeanFactory.getInstance().getBean(ImDepartmentDao.class);
    private ImSessionService imSessionService = BeanFactory.getInstance().getBean(ImSessionService.class);

    @FXML private void sendMsg(ActionEvent actionEvent) {
        if (mainController != null) {
            ThreadPoolUtil.submit(()->{
                try {
                    String userId = UiUtil.getLoginImUser().getUserId();
                    ImSession imSession = sessionDao.getByGroupCodeAndUserId(imGroup.getServerId(), userId);
                    if (imSession == null) {
                        imSession = imSessionService.createSession(userId, imGroup);
                    }
                    ImSession tmpSession = imSession;
                    FxThreadUtil.runLater(()-> {
                        mainController.switchMessageSession(tmpSession, null);
                    });
                } catch (Exception e) {
                    LOGGER.error("加载session失败", e);
                }
            });

        }
    }

    public void setImUser(ImUser imUser) {
        this.imUser = imUser;
        userIdTextField.setText(imUser.getUserId());
        userNameTextField.setText(imUser.getUserName());

        Image image = new Image(this.getClass().getResource("/imgs/head.jpg").toExternalForm());
        if (imUser.getHeadImg() != null) {
            image = UserHeadImageUtil.getHeadImage(imUser.getHeadImg());
        }
        headImageView.setImage(image);
        sexTextField.setText(imUser.getSex().getDesc());
        mobileTextField.setText(imUser.getMobile());
        mottoTextField.setText(imUser.getMotto());

        ThreadPoolUtil.submit(()->{
            try {
                ImDepartment department = departmentDao.getDepartmentByCode(imUser.getDepartmentCode());
                FxThreadUtil.runLater(()->{
                    departmentNameTextField.setText(department.getName());
                });
            } catch (Exception e) {
                LOGGER.error("加载部门失败", e);
            }
        });

    }

    public void setImGroup(ImGroup imGroup) {
        this.imGroup = imGroup;
        groupNameTextField.setText(imGroup.getName());
        groupDescTextArea.setText(imGroup.getDescription());
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}
